<?php 
	$id = isset($_POST['id']) ? $_POST['id'] : null;
	$name = isset($_POST['name']) ? $_POST['name'] : null;
	$title = isset($_POST['title']) ? $_POST['title'] : null;
	$content = isset($_POST['content']) ? $_POST['content'] : null;
	$method = isset($_POST['method']) ? $_POST['method'] : null;
	
	require_once 'MessageAction.class.php';
	require_once 'ReplyAction.class.php';
	
	$action = new MessageAction();
	$replayAction = new ReplyAction();
	
	if($method == 'add'){
		$msg = new Message();
		$msg -> name = $name;
		$msg -> title = $title;
		$msg -> content = $content;
		$msg -> create_time = time();
		
		if($msg -> id = $action -> add($msg)){
			$msg = $action -> queryById($msg);
			$data = array("id" => $msg -> id,
						"name" => $msg -> name,
						"title" => $msg -> title,
						"content" => $msg -> content,
						"create_time" => $msg -> create_time);
			echo json_encode($data);
		} else {
			echo false;
		}
		
	} else if($method == 'query'){
		$msgs = $action -> queryAll();
		if(count($msgs)>0){
			$data = array();
			foreach ($msgs as $msg) {
				$reply = $replayAction -> queryByMsgId($msg);
				if($reply){
					$r = array("id" => $reply -> id,
								"msg_id" => $reply -> msg_id,
								"admin_id" => $reply -> admin_id,
								"content" => $reply -> content,
								"create_time" => $reply -> create_time);
				} else{
					$r = false;
				}
				$data[] = array("id" => $msg -> id,
								"name" => $msg -> name,
								"title" => $msg -> title,
								"content" => $msg -> content,
								"create_time" => $msg -> create_time,
								"reply" => $r);
			}
			 echo json_encode($data);
		} else {
			echo false;
		}
	} else if($method == 'delete'){
		$msg = new Message();
		$msg -> id = $id;
		if($action -> delete($msg)){
			echo true;
		}else{
			echo false;
		}
	}
	

?>